import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../pages/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    component: index,
    // children: [
    //   { path: '', redirect: '/home' },// 默认子路由：当访问 /index 时自动跳转至 /home
    //   { path: '/home', component: () => import('../pages/extend/home.vue') },
    //   { path: '/init', component: () => import('../pages/cli/init.vue') },
    //   { path: '/run', component: () => import('../pages/cli/run.vue') },
    //   { path: '/plug', component: () => import('../pages/cli/plug.vue') },
    //   { path: '/rule', component: () => import('../pages/cli/rule.vue') },
    // ]
  },
  {
    path: '*',
    redirect: '/index' // 所有未匹配路径重定向到首页
  }
]

// 解决重复导航的错误
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    // 忽略 NavigationDuplicated 错误
    if (err.name !== 'NavigationDuplicated') throw err;
  });
};

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
